<?php
/**
 * 秒杀
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 15:56
 */

namespace WXAPI\Controller;

use Think\Controller;

class MiaoshaController extends BaseController
{
    /**
     * 今日秒杀安排
     */
    public function GoodsLists()
    {
        // $store_id = I('store_id',1);
        $time = intval(date('H'));
        $date = date('Y-m-d');
        $sql = "SELECT * FROM ty_miaosha_goods WHERE `open_time` = '" .$date."' order by start_time";
        $list = M()->query($sql);

        if (empty($list)) {
            exit(json_encode([
                'status' => 0,
                'msg' => '暂无秒杀活动'
            ]));
        }
        // var_dump($store_id);
        $miaosha_set = M('ms_setting')->where(array('id'=>1))->find();
        // var_dump($miaosha_set);
        if(!($miaosha_set['miaosha'] > 0)){
            exit(json_encode([
                'status' => 0,
                'msg' => '暂无秒杀活动'
            ]));
        }
        $has_active = false;
        $miaosha_time_json = M('miaosha_time')->where(array('id'=>1))->find();
        $miaosha_time_array = json_decode($miaosha_time_json['open_time']);
        $arr = array();
        foreach ($list as $k => $v) {
            if(!in_array($list[$k]['start_time'],$miaosha_time_array)){
                unset($list[$k]);
            }else{
                if(!in_array($list[$k]['start_time'],$arr)){
                    $arr[] = $list[$k]['start_time'];
                    if ($list[$k]['start_time'] < $time) {
                        $list[$k]['active'] = false;
                        $list[$k]['status_text'] = '已结束';
                        $list[$k]['status'] = 0;
                    } elseif ($list[$k]['start_time'] == $time) {
                        // 20181105 判断当前是否有点击时间，如果有active的状态变成false，否则active的状态变成true START 亮
                        $my_time = I('get.my_time');
                        if ($my_time){
                            $active = false;
                        }else{
                            $active = true;
                        }
                        $list[$k]['active'] = $active;
                        // 20181105 判断当前是否有点击时间，如果有active的状态变成false，否则active的状态变成true END 亮

//                        $list[$k]['active'] = true;
                        $list[$k]['status_text'] = '进行中';
                        $list[$k]['status'] = 1;
                        $has_active = true;
                    } else {
                        $list[$k]['active'] = false;
                        $list[$k]['status_text'] = '即将开始';
                        $list[$k]['status'] = 2;
                    }
                    $list[$k]['title'] = $v['start_time'] < 10 ? ('0' . $v['start_time'] . ':00') : ($v['start_time'] . ':00');
                    $list[$k]['begin_time'] = strtotime($v['open_date'] . ' ' . $v['start_time'] . ':00:00');
                    $list[$k]['end_time'] = strtotime($v['open_date'] . ' ' . $v['start_time'] . ':59:59');
                    $list[$k]['now_time'] = time();
                }else{
                	unset($list[$k]);
                }
            }
        }
        $nmn = array();
        foreach($list as $v){
        	$nmn[] = $v;
        }
        $list = $nmn;
        if (!$has_active) {
            foreach ($list as $i => $item) {
                if ($item['status']==2){
                    $list[$i]['active'] = true;
                    break;
                }
            }
        }
       
        exit(json_encode([
            'status' => 1,
            'msg' => '秒杀商品获取成功',
            'data' => $list
        ]));
    }

    public function miaoshaGoodsInfo(){

        $msg = M('miaosha_goods')->find(I('id'));

        if($msg){
            $msg['attr'] = json_decode($msg['attr']);
            exit(json_encode(array('status'=>1,'data'=>$msg)));
        }else{
            exit(json_encode(array('status'=>0)));
        }
    }
}